
import axios, { AxiosResponse } from 'axios'
import type { ApiResponse } from '~/types/api'

const api = axios.create({
    baseURL: 'http://111.160.49.98:60',
    // baseURL: 'http://127.0.0.1:8000',
    timeout: 5000
})

export const searchMicTask = async (params: {
    page?: number;
    page_size?: number;
    strain_number?: string;
    genus_name?: string;
    species_name?: string;
    chinese_latin_species_name?: string;
} = {}): Promise<ApiResponse> => {
    try {

        // 设置默认参数
        const defaultParams = {
            page: 1,
            page_size: 999,
            strain_number: '',
            genus_name: '',
            species_name: '',
            chinese_latin_species_name: ''
        };

        // 合并默认参数和传入的参数
        const requestParams = { ...defaultParams, ...params };

        const response: AxiosResponse<ApiResponse> = await api.get('/Species/fileManager/bacterialGet', {
            params: requestParams
        });

        // 根据响应码判断是否成功
        if (response.data.code === 200) {
            return {
                success: true,
                msg: '搜索成功！',
                data: response.data.data,
                error: ''
            }
        } else {
            return {
                success: false,
                msg: response.data.msg || '请求处理失败',
                error: response.data.error || '未知错误'
            }
        }
    } catch (error: any) {
        // 处理请求失败的情况
        return {
            success: false,
            msg: error.response?.data?.msg || '请求失败',
            error: error.message || '网络错误'
        }
    }
}


export const searchMedicTask = async (params: {
    page?: number;
    page_size?: number;
    strain_number?: string;
    medicinal_material_name?: string;
    genus_name?: string;
    species_name?: string;
    alias?: string;
    main_function?: string;
} = {}): Promise<ApiResponse> => {
    try {
        // 设置默认参数
        const defaultParams = {
            page: 1,
            page_size: 999,
            strain_number: '',
            medicinal_material_name: '',  // 药材名
            genus_name: '',
            species_name: '',
            alias: '',  // 别名/俗名
            main_function: ''  // 主要功能
        };

        // 合并默认参数和传入的参数
        const requestParams = { ...defaultParams, ...params };

        const response: AxiosResponse<ApiResponse> = await api.get('/Species/fileManager/medicinalGet', {
            params: requestParams
        });

        // 根据响应码判断是否成功
        if (response.data.code === 200) {
            return {
                success: true,
                msg: '搜索成功！',
                data: response.data.data,
                error: ''
            };
        } else {
            return {
                success: false,
                msg: response.data.msg || '请求处理失败',
                error: response.data.error || '未知错误'
            };
        }
    } catch (error: any) {
        // 处理请求失败的情况
        return {
            success: false,
            msg: error.response?.data?.msg || '请求失败',
            error: error.message || '网络错误'
        };
    }
};


export const getSpeciesRelationTask = async (params: {
    strainType: 'Medicinal' | 'Bacterial';
} = { strainType: 'Medicinal' }): Promise<ApiResponse> => {
    try {
        // 设置默认参数
        const defaultParams = {
            strainType: 'Medicinal',
        };

        // 合并默认参数和传入的参数
        const requestParams = { ...defaultParams, ...params };

        const response: AxiosResponse<ApiResponse> = await api.get('/Species/fileManager/SpeciesGet', {
            params: requestParams
        });

        // 根据响应码判断是否成功
        if (response.data.code === 200) {
            return {
                success: true,
                msg: '获取种属关系成功！',
                data: response.data.data,
                error: ''
            };
        } else {
            return {
                success: false,
                msg: response.data.msg || '请求处理失败',
                error: response.data.error || '未知错误'
            };
        }
    } catch (error: any) {
        // 处理请求失败的情况
        return {
            success: false,
            msg: error.response?.data?.msg || '请求失败',
            error: error.message || '网络错误'
        };
    }
};